home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / stk-3.002 / stk-3 / STk-3.1 / Tk / win / tkWinKey.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-17  |  6.9 KB  |  335 lines

  1. /* 
  2.  * tkWinKey.c --
  3.  *
  4.  *    This file contains X emulation routines for keyboard related
  5.  *    functions.
  6.  *
  7.  * Copyright (c) 1995 Sun Microsystems, Inc.
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution
  10.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  *
  12.  * SCCS: @(#) tkWinKey.c 1.6 96/02/15 18:56:04
  13.  */
  14.  
  15. #include "tkWinInt.h"
  16.  
  17. typedef struct {
  18.     unsigned int keycode;
  19.     KeySym keysym;
  20. } Keys;
  21.  
  22. static Keys keymap[] = {
  23.     VK_CANCEL, XK_Cancel,
  24.     VK_BACK, XK_BackSpace,
  25.     VK_TAB, XK_Tab,
  26.     VK_CLEAR, XK_Clear,
  27.     VK_RETURN, XK_Return,
  28.     VK_SHIFT, XK_Shift_L,
  29.     VK_CONTROL, XK_Control_L,
  30.     VK_MENU, XK_Alt_L,
  31.     VK_PAUSE, XK_Pause,
  32.     VK_CAPITAL, XK_Caps_Lock,
  33.     VK_ESCAPE, XK_Escape,
  34.     VK_SPACE, XK_space,
  35.     VK_PRIOR, XK_Prior,
  36.     VK_NEXT, XK_Next,
  37.     VK_END, XK_End,
  38.     VK_HOME, XK_Home,
  39.     VK_LEFT, XK_Left,
  40.     VK_UP, XK_Up,
  41.     VK_RIGHT, XK_Right,
  42.     VK_DOWN, XK_Down,
  43.     VK_SELECT, XK_Select,
  44.     VK_PRINT, XK_Print,
  45.     VK_EXECUTE, XK_Execute,
  46.     VK_INSERT, XK_Insert,
  47.     VK_DELETE, XK_Delete,
  48.     VK_HELP, XK_Help,
  49.     VK_F1, XK_F1,
  50.     VK_F2, XK_F2,
  51.     VK_F3, XK_F3,
  52.     VK_F4, XK_F4,
  53.     VK_F5, XK_F5,
  54.     VK_F6, XK_F6,
  55.     VK_F7, XK_F7,
  56.     VK_F8, XK_F8,
  57.     VK_F9, XK_F9,
  58.     VK_F10, XK_F10,
  59.     VK_F11, XK_F11,
  60.     VK_F12, XK_F12,
  61.     VK_F13, XK_F13,
  62.     VK_F14, XK_F14,
  63.     VK_F15, XK_F15,
  64.     VK_F16, XK_F16,
  65.     VK_F17, XK_F17,
  66.     VK_F18, XK_F18,
  67.     VK_F19, XK_F19,
  68.     VK_F20, XK_F20,
  69.     VK_F21, XK_F21,
  70.     VK_F22, XK_F22,
  71.     VK_F23, XK_F23,
  72.     VK_F24, XK_F24,
  73.     VK_NUMLOCK, XK_Num_Lock, 
  74.     VK_SCROLL, XK_Scroll_Lock,
  75.     0, NoSymbol
  76. };
  77.  
  78.  
  79. /*
  80.  *----------------------------------------------------------------------
  81.  *
  82.  * XLookupString --
  83.  *
  84.  *    Retrieve the string equivalent for the given keyboard event.
  85.  *
  86.  * Results:
  87.  *    Returns the number of characters stored in buffer_return.
  88.  *
  89.  * Side effects:
  90.  *    Retrieves the characters stored in the event and inserts them
  91.  *    into buffer_return.
  92.  *
  93.  *----------------------------------------------------------------------
  94.  */
  95.  
  96. int
  97. XLookupString(event_struct, buffer_return, bytes_buffer, keysym_return,
  98.     status_in_out)
  99.     XKeyEvent* event_struct;
  100.     char* buffer_return;
  101.     int bytes_buffer;
  102.     KeySym* keysym_return;
  103.     XComposeStatus* status_in_out;
  104. {
  105.     int i, limit;
  106.  
  107.     if ((event_struct->nchars <= 0) || (buffer_return == NULL)) {
  108.     return 0;
  109.     }
  110.     limit = (event_struct->nchars < bytes_buffer) ? event_struct->nchars :
  111.     bytes_buffer;
  112.  
  113.     for (i = 0; i < limit; i++) {
  114.     buffer_return[i] = event_struct->trans_chars[i];
  115.     }
  116.  
  117.     if (keysym_return != NULL) {
  118.     *keysym_return = NoSymbol;
  119.     }
  120.     return i;
  121. }
  122.  
  123. /*
  124.  *----------------------------------------------------------------------
  125.  *
  126.  * XKeycodeToKeysym --
  127.  *
  128.  *    Translate from a system-dependent keycode to a
  129.  *    system-independent keysym.
  130.  *
  131.  * Results:
  132.  *    Returns the translated keysym, or NoSymbol on failure.
  133.  *
  134.  * Side effects:
  135.  *    None.
  136.  *
  137.  *----------------------------------------------------------------------
  138.  */
  139.  
  140. KeySym
  141. XKeycodeToKeysym(display, keycode, index)
  142.     Display* display;
  143.     unsigned int keycode;
  144.     int index;
  145. {
  146.     Keys* key;
  147.     BYTE keys[256];
  148.     int result;
  149.     char buf[4];
  150.     unsigned int scancode = MapVirtualKey(keycode, 0);
  151.  
  152.     memset(keys, 0, 256);
  153.     if (index & 0x02) {
  154.     keys[VK_NUMLOCK] = 1;
  155.     }
  156.     if (index & 0x01) {
  157.     keys[VK_SHIFT] = 0x80;
  158.     }
  159.     result = ToAscii(keycode, scancode, keys, (LPWORD) buf, 0);
  160.  
  161.     /*
  162.      * Keycode mapped to a valid Latin-1 character.  Since the keysyms
  163.      * for alphanumeric characters map onto Latin-1, we just return it.
  164.      */
  165.  
  166.     if (result == 1 && buf[0] >= 0x20) {
  167.     return (KeySym) buf[0];
  168.     }
  169.  
  170.     /*
  171.      * Keycode is a non-alphanumeric key, so we have to do the lookup.
  172.      */
  173.  
  174.     for (key = keymap; key->keycode != 0; key++) {
  175.     if (key->keycode == keycode) {
  176.         return key->keysym;
  177.     }
  178.     }
  179.  
  180.     return NoSymbol;
  181. }
  182.  
  183. /*
  184.  *----------------------------------------------------------------------
  185.  *
  186.  * XKeysymToKeycode --
  187.  *
  188.  *    Translate a keysym back into a keycode.
  189.  *
  190.  * Results:
  191.  *    Returns the keycode that would generate the specified keysym.
  192.  *
  193.  * Side effects:
  194.  *    None.
  195.  *
  196.  *----------------------------------------------------------------------
  197.  */
  198.  
  199. KeyCode
  200. XKeysymToKeycode(display, keysym)
  201.     Display* display;
  202.     KeySym keysym;
  203. {
  204.     Keys* key;
  205.     SHORT result;
  206.  
  207.     if (keysym >= 0x20) {
  208.     result = VkKeyScan(keysym);
  209.     if (result != -1) {
  210.         return (KeyCode) (result & 0xff);
  211.     }
  212.     }
  213.  
  214.     /*
  215.      * Couldn't map the character to a virtual keycode, so do a
  216.      * table lookup.
  217.      */
  218.  
  219.     for (key = keymap; key->keycode != 0; key++) {
  220.     if (key->keysym == keysym) {
  221.         return key->keycode;
  222.     }
  223.     }
  224.     return 0;
  225. }
  226.  
  227. /*
  228.  *----------------------------------------------------------------------
  229.  *
  230.  * XGetModifierMapping --
  231.  *
  232.  *    Fetch the current keycodes used as modifiers.
  233.  *
  234.  * Results:
  235.  *    Returns a new modifier map.
  236.  *
  237.  * Side effects:
  238.  *    Allocates a new modifier map data structure.
  239.  *
  240.  *----------------------------------------------------------------------
  241.  */
  242.  
  243. XModifierKeymap    *
  244. XGetModifierMapping(display)
  245.     Display* display;
  246. {
  247.     XModifierKeymap *map = (XModifierKeymap *)ckalloc(sizeof(XModifierKeymap));
  248.  
  249.     map->max_keypermod = 1;
  250.     map->modifiermap = (KeyCode *) ckalloc(sizeof(KeyCode)*8);
  251.     map->modifiermap[ShiftMapIndex] = VK_SHIFT;
  252.     map->modifiermap[LockMapIndex] = VK_CAPITAL;
  253.     map->modifiermap[ControlMapIndex] = VK_CONTROL;
  254.     map->modifiermap[Mod1MapIndex] = VK_NUMLOCK;
  255.     map->modifiermap[Mod2MapIndex] = VK_MENU;
  256.     map->modifiermap[Mod3MapIndex] = VK_SCROLL;
  257.     map->modifiermap[Mod4MapIndex] = 0;
  258.     map->modifiermap[Mod5MapIndex] = 0;
  259.     return map;
  260. }
  261.  
  262. /*
  263.  *----------------------------------------------------------------------
  264.  *
  265.  * XFreeModifiermap --
  266.  *
  267.  *    Deallocate a modifier map that was created by
  268.  *    XGetModifierMapping.
  269.  *
  270.  * Results:
  271.  *    None.
  272.  *
  273.  * Side effects:
  274.  *    Frees the datastructure referenced by modmap.
  275.  *
  276.  *----------------------------------------------------------------------
  277.  */
  278.  
  279. void
  280. XFreeModifiermap(modmap)
  281.     XModifierKeymap* modmap;
  282. {
  283.     ckfree((char *) modmap->modifiermap);
  284.     ckfree((char *) modmap);
  285. }
  286.  
  287. /*
  288.  *----------------------------------------------------------------------
  289.  *
  290.  * XStringToKeysym --
  291.  *
  292.  *    Translate a keysym name to the matching keysym. 
  293.  *
  294.  * Results:
  295.  *    Returns the keysym.  Since this is already handled by
  296.  *    Tk's StringToKeysym function, we just return NoSymbol.
  297.  *
  298.  * Side effects:
  299.  *    None.
  300.  *
  301.  *----------------------------------------------------------------------
  302.  */
  303.  
  304. KeySym
  305. XStringToKeysym(string)
  306.     _Xconst char *string;
  307. {
  308.     return NoSymbol;
  309. }
  310.  
  311. /*
  312.  *----------------------------------------------------------------------
  313.  *
  314.  * XKeysymToString --
  315.  *
  316.  *    Convert a keysym to character form.
  317.  *
  318.  * Results:
  319.  *    Returns NULL, since Tk will have handled this already.
  320.  *
  321.  * Side effects:
  322.  *    None.
  323.  *
  324.  *----------------------------------------------------------------------
  325.  */
  326.  
  327. char *
  328. XKeysymToString(keysym)
  329.     KeySym keysym;
  330. {
  331.     return NULL;
  332. }
  333.  
  334.  
  335.